﻿//This filter has been tested and seems to work correctly

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// EdgeDetectHorizontal is a filter to work 
    /// out horizontal edges.
    /// </summary>
    public class EdgeDetectHorizontal : BaseImageFilter
    {
        /// <summary>
        /// default constructor
        /// </summary>
        public EdgeDetectHorizontal()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="EdgeDetectHorizontal"/> class.
        /// </summary>
        /// <param name="_edgeDetectHorizontal">The _edge detect horizontal.</param>
        internal EdgeDetectHorizontal(EdgeDetectHorizontal _edgeDetectHorizontal)
        {
        }

        /// <summary>
        /// detect the horizontal Edges.
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bmTemp = (UnsafeBitmap)_bitmap.Clone();

            // GDI+ still lies to us - the return format is BGR, NOT RGB.
            BitmapData bmData = _bitmap.BitmapData; //b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            BitmapData bmData2 = bmTemp.BitmapData; // bmTemp.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int stride = bmData.Stride;
            System.IntPtr Scan0 = bmData.Scan0;
            System.IntPtr Scan02 = bmData2.Scan0;

            unsafe
            {
                byte* p = (byte*)(void*)Scan0;
                byte* p2 = (byte*)(void*)Scan02;

                int nByteCount = _bitmap.BytesPerPixelCount;
                
                int nOffset = stride - _bitmap.Width * nByteCount;  //stride - _bitmap.Width * 3; 
                int nWidth = _bitmap.Width * nByteCount;            // _bitmap.Width * 3;

                int nPixel = 0;

                p += stride;
                p2 += stride;

                int nHeight = _bitmap.Height;
                for (int y = 0; y < nHeight - nByteCount; ++y)
                {
                    p += 9;
                    p2 += 9;

                    for (int x = 9; x < nWidth - 9; ++x)
                    {
                        nPixel = ((p2 + stride - 9)[0] +
                            (p2 + stride - 6)[0] +
                            (p2 + stride - 3)[0] +
                            (p2 + stride)[0] +
                            (p2 + stride + 3)[0] +
                            (p2 + stride + 6)[0] +
                            (p2 + stride + 9)[0] -
                            (p2 - stride - 9)[0] -
                            (p2 - stride - 6)[0] -
                            (p2 - stride - 3)[0] -
                            (p2 - stride)[0] -
                            (p2 - stride + 3)[0] -
                            (p2 - stride + 6)[0] -
                            (p2 - stride + 9)[0]);

                        if (nPixel < 0) nPixel = 0;
                        if (nPixel > 255) nPixel = 255;

                        (p + stride)[0] = (byte)nPixel;

                        ++p;
                        ++p2;
                    }
                    p += 9 + nOffset;
                    p2 += 9 + nOffset;
                }
            }

            bmTemp.UnlockImage();
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new EdgeDetectHorizontal(this);
        }
    }
}
